/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <div class="subject">
        <h3>文章管理</h3>
        <h5>网站系统文章索引与管理</h5>
      </div>
    </div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>文章管理, 由总平台设置管理.</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>
    <div class="mDiv">
      <div class="ftitle">
        <h3>文章列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <div class="fbutton">
        <div class="add" @click="toAdd()">
          <span>
            <i class="el-icon-circle-plus" /> 新增文章
          </span>
        </div>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
      <div class="searchbox">
        <el-cascader
          clearable
          size="small"
          v-model="article_type"
          placeholder="顶级分类"
          :options="articleCats"
          :props="{checkStrictly: true,emitPath: false, value: 'cat_id', label: 'cat_name'}"
        ></el-cascader>
        <el-input class="searchinput" v-model="title" size="small" placeholder="文章标题" clearable></el-input>
        <el-button
          class="searchbtn"
          size="small"
          icon="el-icon-search"
          @click="currentPage = 1;getPage()"
        >搜索</el-button>
      </div>
    </div>

    <div class="tablebox">
      <el-table :data="tableData" ref="multipleTable" style="width: 100%;">
        <el-table-column label="标题" prop="title" width="400" show-overflow-tooltip></el-table-column>
        <el-table-column label="文章分类" prop="article_cat.cat_name" width="150"></el-table-column>
        <el-table-column label="显示" width="100">
          <template slot-scope="scope">
            <div :class="scope.row.is_open == 1?'yes':'no'" @click="update(scope.row)">
              <i :class="scope.row.is_open == 1?'fa fa-check-circle':'fa fa-ban'"></i>
              <span v-text="scope.row.is_open == 1?'是':'否'"></span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" prop="add_time_desc" width="200"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                @click="detail(scope.row.article_id)"
                icon="el-icon-zoom-in"
                size="small"
              >查看</el-button>
              <el-button
                @click="toEdit(scope.row.article_id)"
                icon="el-icon-edit-outline"
                size="small"
              >编辑</el-button>
              <el-button @click="remove(scope.row.article_id)" icon="el-icon-delete" size="small">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getArticlePage,
  getArticleCats,
  updateArticle,
  deleteArticle
} from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 10,
      article_type: null,
      title: null,
      articleCats: []
    };
  },
  created() {
    this.getPage();
    this.getArticleCats();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
      getArticlePage({
        cat_id: that.article_type,
        title: that.title,
        page: that.currentPage,
        size: that.pageSize
      }).then(function(res) {
        that.tableData = res.records;
        that.totalCount = res.total;
      });
    },
    getArticleCats() {
      var that = this;
      getArticleCats().then(function(res) {
        that.articleCats = res;
      });
    },
    refresh() {
      this.getPage();
      this.$message.success("刷新成功！");
    },
    update(row) {
      var that = this;
      row.is_open = row.is_open == 0 ? 1 : 0;
      updateArticle(row).then(function(res) {
        if (res.status == 1) {
          that.getPage();
          that.$message.sussess("操作成功");
        }
      });
    },
    remove(id) {
      var that = this;
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteArticle(id).then(function(res) {
            if (res.status == 1) {
              that.getPage();
              that.$message.success("删除成功!");
            }
          });
        })
        .catch(() => {
          that.$message.info({
            message: "已取消删除"
          });
        });
    },
    detail() {
      this.$message.info("开发中...");
    },
    toAdd() {
      this.$router.push({
        name: "article_add",
        currentPage: this.currentPage
      });
    },
    toEdit(id) {
      this.$router.push({
        name: "article_edit",
        params: {
          id: id,
          currentPage: this.currentPage
        }
      });
    }
  }
};
</script>

<style scoped>
.searchinput {
  width: auto;
}
</style>
